/**
* 左边菜单
*/
<template>
  <el-menu
    :default-active="$route.path"
    :collapse="collapsed"
    collapse-transition
    router
    unique-opened
    class="el-menu-vertical-demo"
    background-color="#545c64"
    text-color="#fff"
    active-text-color="#ffd04b"
  >
    <div class="logobox">
      <img
        v-show="displayed"
        class="logoimg"
        src="../assets/img/logo2.png"
        alt=""
      />
    </div>
    <el-submenu v-for="menu in allmenu" :key="menu.id" :index="menu.menuname">
      <template slot="title">
        <i :class="menu.icon"></i>
        <span>{{ menu.menuname }}</span>
      </template>
      <el-menu-item-group>
        <el-menu-item
          v-for="chmenu in menu.menus"
          :index="'/' + chmenu.url"
          :key="chmenu.id"
        >
          <i :class="chmenu.icon"></i>
          <span>{{ chmenu.menuname }}</span>
        </el-menu-item>
      </el-menu-item-group>
    </el-submenu>
  </el-menu>
</template>
<script>
export default {
  name: 'leftnav',
  data() {
    return {
      collapsed: false,
      allmenu: [],
      displayed: true,
    }
  },
  // 创建完毕状态(里面是操作)
  created() {
    this.getMenu(JSON.parse(localStorage.getItem('userdata')).id);
    // 监听
    this.$root.Bus.$on('toggle', value => {
      this.collapsed = !value
    })
    this.$root.Bus.$on('logo', value => {
      this.displayed = value
    })
  },
  methods: {
    getMenu(id) {
      this.$axios.get("/api/menu/getList/" + id).then(res => {
        this.allmenu = res.data.data;
      }).catch(() => {
        this.$message.error('菜单加载失败，请重新登录！')
      })
    }
  }
}
</script>
<style>
.el-menu-vertical-demo:not(.el-menu--collapse) {
  width: 240px;
  min-height: 400px;
}
.el-menu-vertical-demo:not(.el-menu--collapse) {
  border: none;
  text-align: left;
}
.el-menu-item-group__title {
  padding: 0px;
}
.el-menu-bg {
  background-color: #1f2d3d !important;
}
.el-menu {
  border: none;
}
.logobox {
  height: 40px;
  line-height: 40px;
  color: #9d9d9d;
  font-size: 20px;
  text-align: center;
  padding: 20px 0px;
}
.logoimg {
  height: 800%;
  position: relative;
  top: -350%;
  left: -18%;
  cursor: pointer;
}
</style>
